<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="采购计划名称" prop="planName">
        <el-input
          v-model="queryParams.planName"
          placeholder="请输入采购计划名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="采购计划编号(系统自动生成)" prop="planNo">
        <el-input
          v-model="queryParams.planNo"
          placeholder="请输入采购计划编号(系统自动生成)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item
        label="采购类型SERVICE/EQUIPMENT/ENGINEERING/MATERIAL/DIRECT/MINOR"
        prop="purchaseType"
      >
        <el-select
          v-model="queryParams.purchaseType"
          placeholder="请选择采购类型SERVICE/EQUIPMENT/ENGINEERING/MATERIAL/DIRECT/MINOR"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PURCHASE_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预算归属部门ID" prop="budgetDepartmentId">
        <el-input
          v-model="queryParams.budgetDepartmentId"
          placeholder="请输入预算归属部门ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="预算归属部门名称" prop="budgetDepartmentName">
        <el-input
          v-model="queryParams.budgetDepartmentName"
          placeholder="请输入预算归属部门名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="需求申请部门ID" prop="applicationDepartmentId">
        <el-input
          v-model="queryParams.applicationDepartmentId"
          placeholder="请输入需求申请部门ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="需求申请部门名称" prop="applicationDepartmentName">
        <el-input
          v-model="queryParams.applicationDepartmentName"
          placeholder="请输入需求申请部门名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="需求申请人ID" prop="applicantId">
        <el-input
          v-model="queryParams.applicantId"
          placeholder="请输入需求申请人ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="需求申请人姓名" prop="applicantName">
        <el-input
          v-model="queryParams.applicantName"
          placeholder="请输入需求申请人姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="采购方式" prop="purchaseMethod">
        <el-select
          v-model="queryParams.purchaseMethod"
          placeholder="请选择采购方式"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.PURCHASE_METHOD)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="质保期/维保期" prop="warrantyPeriod">
        <el-input
          v-model="queryParams.warrantyPeriod"
          placeholder="请输入质保期/维保期"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="原合同期限开始时间" prop="originalContractStartTime">
        <el-date-picker
          v-model="queryParams.originalContractStartTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="原合同期限结束时间" prop="originalContractEndTime">
        <el-date-picker
          v-model="queryParams.originalContractEndTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="原合同金额(万元)" prop="originalContractAmount">
        <el-input
          v-model="queryParams.originalContractAmount"
          placeholder="请输入原合同金额(万元)"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="合同履约情况" prop="contractPerformance">
        <el-input
          v-model="queryParams.contractPerformance"
          placeholder="请输入合同履约情况"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="本次采购预估单价来源说明" prop="estimatedPriceSource">
        <el-input
          v-model="queryParams.estimatedPriceSource"
          placeholder="请输入本次采购预估单价来源说明"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="需到货时间" prop="requiredDeliveryTime">
        <el-date-picker
          v-model="queryParams.requiredDeliveryTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="施工工期描述" prop="constructionPeriod">
        <el-input
          v-model="queryParams.constructionPeriod"
          placeholder="请输入施工工期描述"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="施工工期开始时间" prop="constructionStartTime">
        <el-date-picker
          v-model="queryParams.constructionStartTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="施工工期结束时间" prop="constructionEndTime">
        <el-date-picker
          v-model="queryParams.constructionEndTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="施工期间负责人" prop="constructionLeader">
        <el-input
          v-model="queryParams.constructionLeader"
          placeholder="请输入施工期间负责人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否含有非标配件(0-否 1-是)" prop="hasNonStandardParts">
        <el-select
          v-model="queryParams.hasNonStandardParts"
          placeholder="请选择是否含有非标配件(0-否 1-是)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否含暂列金(0-否 1-是)" prop="hasProvisionalSum">
        <el-select
          v-model="queryParams.hasProvisionalSum"
          placeholder="请选择是否含暂列金(0-否 1-是)"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="实施小组组成及分工" prop="implementationTeam">
        <el-input
          v-model="queryParams.implementationTeam"
          placeholder="请输入实施小组组成及分工"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="谈判报价方式" prop="negotiationMethod">
        <el-input
          v-model="queryParams.negotiationMethod"
          placeholder="请输入谈判报价方式"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="供应商选择原则" prop="supplierSelectionPrinciple">
        <el-input
          v-model="queryParams.supplierSelectionPrinciple"
          placeholder="请输入供应商选择原则"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="项目理由及必要性说明" prop="projectReason">
        <el-input
          v-model="queryParams.projectReason"
          placeholder="请输入项目理由及必要性说明"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="备注说明" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注说明"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="采购申请开始时间" prop="applicationStartTime">
        <el-date-picker
          v-model="queryParams.applicationStartTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="采购申请结束时间" prop="applicationEndTime">
        <el-date-picker
          v-model="queryParams.applicationEndTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['erp:purchase-plan:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:purchase-plan:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="采购计划名称" align="center" prop="planName" />
      <el-table-column label="采购计划编号(系统自动生成)" align="center" prop="planNo" />
      <el-table-column
        label="采购类型SERVICE/EQUIPMENT/ENGINEERING/MATERIAL/DIRECT/MINOR"
        align="center"
        prop="purchaseType"
      >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PURCHASE_TYPE" :value="scope.row.purchaseType" />
        </template>
      </el-table-column>
      <el-table-column label="预算归属部门ID" align="center" prop="budgetDepartmentId" />
      <el-table-column label="预算归属部门名称" align="center" prop="budgetDepartmentName" />
      <el-table-column label="需求申请部门ID" align="center" prop="applicationDepartmentId" />
      <el-table-column label="需求申请部门名称" align="center" prop="applicationDepartmentName" />
      <el-table-column label="需求申请人ID" align="center" prop="applicantId" />
      <el-table-column label="需求申请人姓名" align="center" prop="applicantName" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="采购方式" align="center" prop="purchaseMethod">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.PURCHASE_METHOD" :value="scope.row.purchaseMethod" />
        </template>
      </el-table-column>
      <el-table-column label="质保期/维保期" align="center" prop="warrantyPeriod" />
      <el-table-column
        label="原合同期限开始时间"
        align="center"
        prop="originalContractStartTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="原合同期限结束时间"
        align="center"
        prop="originalContractEndTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="原合同金额(万元)" align="center" prop="originalContractAmount" />
      <el-table-column label="合同履约情况" align="center" prop="contractPerformance" />
      <el-table-column
        label="本次采购预估单价来源说明"
        align="center"
        prop="estimatedPriceSource"
      />
      <el-table-column
        label="需到货时间"
        align="center"
        prop="requiredDeliveryTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="施工工期描述" align="center" prop="constructionPeriod" />
      <el-table-column
        label="施工工期开始时间"
        align="center"
        prop="constructionStartTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="施工工期结束时间"
        align="center"
        prop="constructionEndTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="施工期间负责人" align="center" prop="constructionLeader" />
      <el-table-column
        label="是否含有非标配件(0-否 1-是)"
        align="center"
        prop="hasNonStandardParts"
      />
      <el-table-column label="是否含暂列金(0-否 1-是)" align="center" prop="hasProvisionalSum" />
      <el-table-column label="实施小组组成及分工" align="center" prop="implementationTeam" />
      <el-table-column label="谈判报价方式" align="center" prop="negotiationMethod" />
      <el-table-column label="供应商选择原则" align="center" prop="supplierSelectionPrinciple" />
      <el-table-column label="项目理由及必要性说明" align="center" prop="projectReason" />
      <el-table-column label="备注说明" align="center" prop="remark" />
      <el-table-column
        label="采购申请开始时间"
        align="center"
        prop="applicationStartTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="采购申请结束时间"
        align="center"
        prop="applicationEndTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:purchase-plan:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:purchase-plan:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PurchasePlanForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { PurchasePlanApi, PurchasePlanVO } from '@/api/erp/purchaseplan'
import PurchasePlanForm from './PurchasePlanForm.vue'

/** 采购计划 列表 */
defineOptions({ name: 'PurchasePlan' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PurchasePlanVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  planName: undefined,
  planNo: undefined,
  purchaseType: undefined,
  budgetDepartmentId: undefined,
  budgetDepartmentName: undefined,
  applicationDepartmentId: undefined,
  applicationDepartmentName: undefined,
  applicantId: undefined,
  applicantName: undefined,
  projectName: undefined,
  purchaseMethod: undefined,
  warrantyPeriod: undefined,
  originalContractStartTime: [],
  originalContractEndTime: [],
  originalContractAmount: undefined,
  contractPerformance: undefined,
  estimatedPriceSource: undefined,
  requiredDeliveryTime: [],
  constructionPeriod: undefined,
  constructionStartTime: [],
  constructionEndTime: [],
  constructionLeader: undefined,
  hasNonStandardParts: undefined,
  hasProvisionalSum: undefined,
  implementationTeam: undefined,
  negotiationMethod: undefined,
  supplierSelectionPrinciple: undefined,
  projectReason: undefined,
  remark: undefined,
  applicationStartTime: [],
  applicationEndTime: [],
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PurchasePlanApi.getPurchasePlanPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PurchasePlanApi.deletePurchasePlan(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PurchasePlanApi.exportPurchasePlan(queryParams)
    download.excel(data, '采购计划.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>